package org.doc.noliferadio;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NoLifeRadio {

    public static String getTitle() {
        return NLRInfo.getSong();
    }

    public static boolean getOnline() {
        return NLRInfo.getOnline();
    }

    public static int getListeners() {
        return NLRInfo.getListeners();
    }

    private static class NLRInfo {

        public static String getSong() {
            long startt = System.currentTimeMillis();
            try {
                BufferedReader read = new BufferedReader(new InputStreamReader(new URL("http://nolife-radio.com/server_info.json").openStream()));
                String line = read.readLine();
                read.close();
                line = line.substring(line.indexOf("\"song\":\"") + 8, line.indexOf(",\"listeners\"") - 1);
                return line;
            } catch (Exception ex) {
                Logger.getLogger(NoLifeRadio.class.getName()).log(Level.SEVERE, null, ex);
            }
            return "HERPUS DERPUS SOMETHING BORKED";
        }

        public static boolean getOnline() {
            try {
                BufferedReader read = new BufferedReader(new InputStreamReader(new URL("http://nolife-radio.com/server_info.json").openStream()));
                String line = read.readLine();
                read.close();
                line = line.substring(line.indexOf("{\"online\":") + 10, line.indexOf(",\"song\":"));
                return line.equals("true");
            } catch (Exception ex) {
                Logger.getLogger(NoLifeRadio.class.getName()).log(Level.SEVERE, null, ex);
            }
            return false;
        }

        public static int getListeners() {

            try {
                BufferedReader read = new BufferedReader(new InputStreamReader(new URL("http://nolife-radio.com/server_info.json").openStream()));
                String line = read.readLine();
                read.close();
                line = line.substring(line.indexOf("listeners\":") + 12, line.lastIndexOf("}"));
                return Integer.parseInt(line);
            } catch (Exception ex) {
                Logger.getLogger(NoLifeRadio.class.getName()).log(Level.SEVERE, null, ex);
            }
            return -1;
        }
    }
}